package com.foton.mybatis.model;

import com.foton.mybatis.factory.MapperProxyFactory;
import com.foton.mybatis.session.SqlSession;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author RenPu
 * @Date 2023/3/29 15:58
 * @Version 1.0
 * @Description:
 **/
public class MapperRegistry<T> {


    private final Map<Class<T>, MapperProxyFactory<T>> knownMappers=new HashMap<>();


    /**
     * 注册代理工厂
     * @param type
     * @param
     */
    public void addMapper(Class<T> type) {
        this.knownMappers.put(type,new MapperProxyFactory<T>(type));
    }


    /**
     * 获取代理工厂实例
     * @param type
     * @param sqlSession
     * @param <T>
     * @return
     */
    public <T> T getMapper(Class<T> type, SqlSession sqlSession) {
        MapperProxyFactory<T> mapperProxyFactory=(MapperProxyFactory<T>)this.knownMappers.get(type);
        return mapperProxyFactory.newInstance(sqlSession);
    }
}
